package com.wyg.blog.service.serviceimpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wyg.blog.mapper.BlogMapper;
import com.wyg.blog.model.blog.Blog;
import com.wyg.blog.service.blog.BlogService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: WYG
 * @time: 2020/7/2 21:14
 */
@Service
@Transactional
@CacheConfig(cacheNames = "redisCache")
public class BlogServiceImpl implements BlogService {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    BlogMapper blogMapper;

    @Override
    public IPage<Blog> selectSomeBlogs(Page<?> page, String username) {
        return blogMapper.getSomeBlogs(page, username);
    }

    @Override
    public IPage<Blog> selectBolgByLike(Page<?> page, String username, String blog) {
        return blogMapper.getBlogByLike(page, username, blog);
    }

    @Override
    public List<Blog> selectLatestBlog() {
        return blogMapper.getLatestBlog();
    }

    @Override
    public Integer selectBlogCount(Integer type_id) {
        return blogMapper.getBlogCount(type_id);
    }

    @Override
    public Integer selectAllBlogsCount() {
        return blogMapper.getAllBlogsCount();
    }

    @Override
    public IPage<Blog> selectPublishedBlog(Page<?> page, boolean published, String username) {
        return blogMapper.getPublishedBlog(page, published, username);
    }

    @Override
    public IPage<Blog> selectAllBlogs(Page<?> page) {
        return blogMapper.getAllBlogs(page);
    }

    @Override
    @Cacheable(key = "'redis_blog' + #id")
    public Blog selectBlogById(Integer id) {
        logger.info("运行了selectBolg方法,未通过缓存");
        return blogMapper.getBlogById(id);
    }

    @Override
    public IPage<Blog> selectBlogByTypeId(Page<?> page, Integer type_id) {
        return blogMapper.getBlogByTypeId(page, type_id);
    }

    @Override
    public IPage<Blog> selectBlogByTagId(Page<?> page, Integer tag_id) {
        return blogMapper.getBlogByTagId(page, tag_id);
    }

    @Override
    public String selectTitleById(Integer id) {
        return blogMapper.getTitleById(id);
    }

    @Override
    public Integer insertBlog(Blog blog) {
        boolean res = blogMapper.addBlog(blog);
        return blog.getId();
    }

    @Override
    public boolean renewBlog(Integer id, String title, Integer type_id, String description, String content) {
        return blogMapper.updateBlog(id, title, type_id, description, content);
    }

    @Override
    public boolean renewViews(Integer id) {
        return blogMapper.updateViews(id);
    }

    @Override
    public boolean renewLikes(Integer id) {
        return blogMapper.updateLikes(id);
    }

    @Override
    public boolean publishBlog(Integer id) {
        return blogMapper.publishBlog(id);
    }

    @Override
    public boolean lockBlog(Integer id) {
        return blogMapper.lockBlog(id);
    }

    @Override
    public boolean removeBlog(Integer id) {
        return blogMapper.deleteBlog(id);
    }

    @Override
    public String selectContent(Integer id) {
        return blogMapper.getContentById(id);
    }

    @Override
    public boolean insertContent(Integer id, String content) {
        return blogMapper.addContent(id, content);
    }

    @Override
    public boolean renewContent(Integer id, String content) {
        return blogMapper.updateContent(id, content);
    }

    @Override
    public IPage<Map<String, String>> selectBlogInfo(Page<?> page) {
        return blogMapper.getBlogInfo(page);
    }
}
